#include "gvj.h"

std::string data_time() {
	char s[30];
	time_t t = time(NULL);
	struct tm *cur = localtime(&t);
	sprintf(s,"%d-%02d-%02d %02d:%02d:%02d",
		1900+cur->tm_year,1+cur->tm_mon,cur->tm_mday,
		cur->tm_hour,cur->tm_min,cur->tm_sec);
	return std::string(s);
}
	
std::string args_encode(std::vector<PSS> &data) {
	std::string str;
	std::vector<PSS>::iterator it;
	for (it = data.begin(); it != data.end(); it++) {
		if (it != data.begin()) str += "&";
		str += url_encode((*it).first.data()) + "=" + url_encode((*it).second.data()); 
	}
	return str;
}
	
std::string add_comment() {
	std::string comment = std::string("/********************************\n") + 
					   	  std::string(" * submited by GVJ\n") + 
					      std::string(" * time: ") + data_time() + std::string("\n") + 
					      std::string(" *******************************/\n");
	return comment;
}
	
std::string itoa_string(int x) {
	char str[50];
	sprintf(str, "%d", x);
	return std::string(str);
}
	
std::string url_encode(const char *str) {
	int n = strlen(str),m = 0;
	char *s = new char[n*3+10];
	for (int i = 0; i < n; i++, m+=3) {
		sprintf(s+m, "%%%02X", (unsigned char)str[i]);
	}
	std::string res(s);
	delete[] s;
	return res;
}
	
std::string get_cookie_path(const char *dir, const char *oj, int id) {
	char s[500];
	sprintf(s, "%s%s%d.cookie", dir, oj, id);
	return std::string(s);
}
	
std::string random_str(int length) {
	char *s = new char[length+1];
	srand((int)time(0));
	for (int i = 0; i < length; i++) {
		int k = rand() % 2;
		if (k) s[i] = '0' + rand() % 10;
		else s[i] = 'A' + rand() % 26;
	}
	std::string str(s,length);
	delete[] s;
	return str;
}

/*
int set_pipe(int *listen_to_proxy, int *proxy_to_listen, int *in_pipe, int *out_pipe) {
	close(listen_to_proxy[1]);
	close(proxy_to_listen[0]);
	*in_pipe = listen_to_proxy[0];
	*out_pipe = proxy_to_listen[1];
}
*/

int set_process_name(const char *new_name) {
	return prctl(PR_SET_NAME, new_name, 0, 0, 0);
}


// 用于测试的函数，伪造一个从监听进程到代理进程字节流
std::string test_create_stream_to_proxy(const char *oj_name) {
	char buff[10500];
	char length[20];
	const int socket_fd	   = 20;
	if (!strcmp(oj_name, "poj")) {
		const char *problem_id = "1000";
		const char *language   = "G++";
		const char *source = "/*create_by_(test_create_stream_to_proxy)*/\n"
							 "int main() { return 0; }";
		sprintf(buff, "%d|%s|%s|%s|%s|", socket_fd, oj_name, 
				problem_id, language, source);
		sprintf(length, "%d|", strlen(buff));
		return std::string(length) + std::string(buff);
	}
	else if (!strcmp(oj_name, "hdu")) { return std::string(); }
	else if (!strcmp(oj_name, "codeforces")) { return std::string(); }
	else return std::string();
}

int init_python() {
	Py_Initialize();
	int res = Py_IsInitialized();
	if (!res) return res;
	// 必须的，更改环境变量
	PyRun_SimpleString("import sys");
    //PyRun_SimpleString("sys.path.append('./')");
    PyRun_SimpleString("sys.path.append('../html_parser/')");
	return res;
}

void finalize_python() {
	Py_Finalize();
}

bool is_positive_integer(const char *str) {
	if (!str) return false;
	for (int i = 0; str[i]; i++)
		if (!isdigit(str[i])) return false;
	return true;
}

char* strlower(char *str) {
	if (!str) return str;
	for (int i = 0; str[i]; i++)
		tolower(str[i]);
	return str;
}

char* strupper(char *str) {
	if (!str) return str;
	for (int i = 0; str[i]; i++)
		toupper(str[i]);
	return str;
}

int set_non_blocking(int fd) {
	int flags = fcntl(fd, F_GETFL, 0);
	flags |= O_NONBLOCK;
	fcntl(fd, F_SETFL, flags);
	return fd;
}
